﻿using System.Linq;
using HighDiveEngine.Model;
using HighDiveEngine.Resources;
using HighDiveAdmin.Engine;
using NUnit.Framework;
using UnitTestHighDive;

namespace ModelTestHighDive
{
    [TestFixture]
    public class ModelTestHighDiveEngine
    {
        HighDiveAdminEngine _he = new HighDiveAdminEngine();

        [SetUp]
        public void Setup()
        {
            UnitTestMockObjects.ResetDatabase();
            UnitTestMockObjects.ResetObjects();
        }

        [Test]
        public void ContestSimulationHighDiveEngine()
        {
            _he.NewContest();
            _he.SetNumJudges(3);

            _he.Contest.Name = "contest1";
            _he.AddDive(UnitTestMockObjects._dive11);
            _he.AddDive(UnitTestMockObjects._dive12);
            _he.AddDive(UnitTestMockObjects._dive21);
            _he.AddDive(UnitTestMockObjects._dive22);
            _he.AddDive(UnitTestMockObjects._dive31);
            _he.AddDive(UnitTestMockObjects._dive32);
            _he.AddJudge(UnitTestMockObjects._judge1);
            _he.AddJudge(UnitTestMockObjects._judge2);
            _he.AddJudge(UnitTestMockObjects._judge3);            
            _he.SaveCompleteContest();
            _he.ResetContest();

            //clear contest in HighDiveEngine then load from DB
            _he.NewContest();
            _he.LoadContest(1);
            Assert.AreEqual("contest1", _he.Contest.Name);

            _he.Contest.Status = ContestStatus.Started;

            int jumpCount = _he.Contest.DiveQueue.Count;

            int i = 0;
            //Loop som väljer det hopp som är först i kön, och sätter sedan poäng på hoppet.
            while (_he.Contest.DiveQueue.Count > 0)
            {
                var firstDive = _he.Contest.DiveQueue.First();
                _he.UpdateDiveScore(new DiveScore { Judge = _he.Contest.JudgeList[0], Score = 1.0 }, firstDive);
                _he.UpdateDiveScore(new DiveScore { Judge = _he.Contest.JudgeList[1], Score = 2.0 }, firstDive);
                _he.UpdateDiveScore(new DiveScore { Judge = _he.Contest.JudgeList[2], Score = 3.0 }, firstDive);
                i++;
                Assert.AreEqual(i, _he.Contest.DiveHistory.Count);
                Assert.AreEqual(jumpCount - i, _he.Contest.DiveQueue.Count);
            }

            Assert.AreEqual(0, _he.Contest.DiveQueue.Count);
            Assert.AreEqual(jumpCount, _he.Contest.DiveHistory.Count);
        }
    }
}
